package com.base.utils;

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * http请求工具类
 */
public class HttpRequsetUtil {


    //医保后台服务器地址
    private static String url ="http://192.168.127.99:8088";
    private static Logger logger = LoggerFactory.getLogger(HttpRequsetUtil.class);

    /**
     * Post请求
     * @param JsonStr
     * @return
     */
    public static String doPost(String JsonStr) {
        String result = "后台请求异常！";
        msg("请求入参:"+JsonStr);
        // 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //String data = "{\"action\":\"SY_JINBAO_CALL\",\"args\":{\"commandSet\":[{\"areaCode\":\"12103\",\"jybh\":\"1100\",\"jysrParam\":\"1100^12103^10882^^20191204235830^0000^20191201|20191204|^\"}]}}";
        JsonStr = JsonStr.replaceAll("\"", "\\\"");
        StringEntity requestEntity = new StringEntity(JsonStr, "utf-8");
        requestEntity.setContentEncoding("UTF-8");
        // {"action":"SY_JINBAO_CALL","args":{"commandSet":[{"areaCode":"12103","jybh":"1100","jysrParam":"1100^12103^10882^^20191204235830^0000^20191201|20191204|^"}]}}
        String url = "http://192.168.127.99:8088";
        // 创建Post请求
        HttpPost httpPost = new HttpPost(url);
        // 设置ContentType(注:如果只是传普通参数的话,ContentType不一定非要用application/json)
        httpPost.setHeader("Content-Type", "application/json;charset=utf8");
        httpPost.setEntity(requestEntity);

        // 响应模型
        CloseableHttpResponse response = null;
        try {
            // 由客户端执行(发送)Post请求
            msg("正在调用医保接口,等待返回交易结果,请稍后...");
            response = httpClient.execute(httpPost);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();

            msg("响应状态为:" + response.getStatusLine());
            if (responseEntity != null) {
                msg("响应内容长度为:" + responseEntity.getContentLength());
                result = EntityUtils.toString(responseEntity);
                msg("响应内容为:" + (result.length()>4000?result.substring(0,4000):result));
            }

        } catch (ClientProtocolException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return result;
    }

    private static void msg(String s) {
        logger.info(s);
       // WebSocketServer.sendInfo(s,ThreadLocalUtil.getSessionId());
    }
}
